import delimited dat

xtset id wave

egen geslacht2=group(geslacht)
*1 = female, 2 = male
egen married2=group(married)
*1 = married, 2 = never, 3 = previously
egen urban2=group(urban)
*1 = rural, 2 = suburban, 3 = urban
egen education2=group(education)
*1 = college, 2 = HS, 3 = Less than HS, 4 = Other, 5 = Vocational
egen job2=group(job)
*1 = employed, 2 = homemaker, 3 = in school, 4 = other, 5 = pensioner, 6 = unemployed 
egen co2=group(cohort2)
egen co5=group(cohort5)

*ssc install gr0070
set scheme plotplain

*average gender attitude by cohort - Figure 2a
xtreg fullgen i.co5 i.geslacht2 i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, re vce(bootstrap, seed(27))

margins, over(co5)
marginsplot, plotopts(lcolor(white)) ytitle("Mean Gender Attitude") xtitle("Birth Cohort") title("")  xlabel(1 "< 1950" 2 "1950-9" 3 "1960-9" 4 "1970-9" 5 "1980+") ylabel(-.4(.1).6) yline(0, lpattern(shortdash))

*average immigration attitude by cohort - Figure 3a
xtreg im i.co5 i.geslacht2 i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, re vce(bootstrap, seed(27))

margins, over(co5)
marginsplot, plotopts(lcolor(white)) ytitle("Mean Immigration Attitude") xtitle("Birth Cohort") title("")  xlabel(1 "< 1950" 2 "1950-9" 3 "1960-9" 4 "1970-9" 5 "1980+") ylabel(-.4(.1).6) yline(0, lpattern(shortdash))

*average gender attitude by wave for older cohorts (have to subset because it's FE) - Figure 4a and Model 1, Table 1

preserve
drop if co5 == 5

xtreg fullgen i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, fe vce(bootstrap, seed(27))

margins, at(wave = (1(1)11) job = 1 x101 = 5.4 religion = 2.1 education = 3 income = 4 urban = 2 married = 1)
marginsplot, title(" ") xtitle("Panel Wave") ytitle("Mean Gender Attitude")xlabel(1 "2007-8" 2 "2008-9" 3 "2009-10" 4 "2010-1" 5 "2011-2" 6 "2012-3" 7 "2013-4" 8 "2015-6" 9 "2016-7" 10 "2017-8" 11 "2018-9") ylabel(-.4(.1).2) yline(0, lpattern(shortdash)) recastci(rarea) recast(line) ciopts(color(gray%20))

*average immigration attitude by wave for older cohorts - Figure 5a and Model 4, Table 1
xtreg im i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, fe vce(bootstrap, seed(27))

margins, at(wave = (1(1)11) job = 1 x101 = 5.4 religion = 2.1 education = 3 income = 4 urban = 2 married = 1)
marginsplot, title(" ") xtitle("Panel Wave") ytitle("Mean Immigration Attitude")xlabel(1 "2007-8" 2 "2008-9" 3 "2009-10" 4 "2010-1" 5 "2011-2" 6 "2012-3" 7 "2013-4" 8 "2015-6" 9 "2016-7" 10 "2017-8" 11 "2018-9") ylabel(-.4(.1).2) yline(0, lpattern(shortdash)) recastci(rarea) recast(line) ciopts(color(gray%20))

restore
preserve
drop if co5 != 5
*average gender attitude by wave for younger cohorts - Figure 4b and Model 2, Table 1
xtreg fullgen i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, fe vce(bootstrap, seed(27))

margins, at(wave = (1(1)11) job = 1 x101 = 5.4 religion = 2.1 education = 3 income = 4 urban = 2 married = 1)
marginsplot, title(" ") xtitle("Panel Wave") ytitle("Mean Gender Attitude")xlabel(1 "2007-8" 2 "2008-9" 3 "2009-10" 4 "2010-1" 5 "2011-2" 6 "2012-3" 7 "2013-4" 8 "2015-6" 9 "2016-7" 10 "2017-8" 11 "2018-9") ylabel(-.4(.1).2) yline(0, lpattern(shortdash)) recastci(rarea) recast(line) ciopts(color(gray%20))

*average immigration attitude by wave for younger cohorts - Figure 5b and Model 5, Table 1
xtreg im i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, fe vce(bootstrap, seed(27))

margins, at(wave = (1(1)11) job = 1 x101 = 5.4 religion = 2.1 education = 3 income = 4 urban = 2 married = 1)
marginsplot, title(" ") xtitle("Panel Wave") ytitle("Mean Immigration Attitude")xlabel(1 "2007-8" 2 "2008-9" 3 "2009-10" 4 "2010-1" 5 "2011-2" 6 "2012-3" 7 "2013-4" 8 "2015-6" 9 "2016-7" 10 "2017-8" 11 "2018-9") ylabel(-.4(.1).2) yline(0, lpattern(shortdash)) recastci(rarea) recast(line) ciopts(color(gray%20))

restore
*Model 3, Table 1
xtreg fullgen i.co5 c.wave##c.wave##c.wave, re vce(bootstrap, seed(27))
eststo gencohort

*Model 6, Table 1
xtreg im i.co5 c.wave##c.wave##c.wave, re vce(bootstrap, seed(27))
eststo imcohort

*Figure 6
xtreg fullgen c.im##i.co2 i.geslacht2 i.married2 i.urban2 income i.job2 i.education2 x101 religion c.wave##c.wave##c.wave, re vce(bootstrap, seed(27))
eststo full 
margins, at(im = (-3.4(.1)2.9)) over(co2) 
marginsplot, recast(line) ci1opts(color(gray%20)) ci2opts(color(gray%70)) recastci(rarea) plot1opts(lpattern(".")) plot2opts(lpattern("solid")) title("") xtitle("Immigration Attitude") ytitle("Predicted Gender Attitude") legend(order(1 "Prior to 1970" 2 "1970 onward")) xlabel(-3.5(.5)3) ylabel(-.75(.25).5) yline(0, lpattern(shortdash))

*appendix
xtreg fullgen c.im##i.co5 i.geslacht2 i.married2 i.urban2 i.job2 income i.education2 x101 religion c.wave##c.wave##c.wave, re vce(bootstrap, seed(27))
eststo co5mod
margins, at(im = (-3.4(.1)2.9)) over(co5) 
marginsplot, by(co5, label("< 1950" "1950-9" "1960-9" "1970-9" "1980+")) recast(line) ci1opts(color(gray%20))  recastci(rarea) plot1opts(lpattern(".")) title("") xtitle("Immigration Attitude") ytitle("Predicted Gender Attitude") legend(order(1 "Prior to 1970" 2 "1970 onward")) xlabel(-3.5(.5)3) ylabel(-.75(.25).5) yline(0, lpattern(shortdash))

xtreg fullgen c.im##i.co2##c.wave##c.wave##c.wave  i.job2 i.geslacht2 i.married2 i.urban2 income i.education2 x101 religion , re vce(bootstrap, seed(27))

margins, at(im = (-3.4(.1)2.9) wave = (1 11)) over(co2) 
marginsplot, by(wave, label("Wave 1 (2008-8)" "Wave 11 (2018-9)"))recast(line) ci1opts(color(gray%20)) ci2opts(color(gray%70)) recastci(rarea) plot1opts(lpattern(".")) plot2opts(lpattern("solid")) title("") xtitle("Immigration Attitude") ytitle("Predicted Gender Attitude") legend(order(1 "Prior to 1970" 2 "1970 onward")) xlabel(-3.5(.5)3) ylabel(-.75(.25).5) yline(0, lpattern(shortdash))

margins, at(im = (-3.4(.1)2.9) wave = 11) over(co2) 
marginsplot, recast(line) ci1opts(color(gray%20)) ci2opts(color(gray%70)) recastci(rarea) plot1opts(lpattern(".")) plot2opts(lpattern("solid")) title("Wave 11 (2018-9)") xtitle("Immigration Attitude") ytitle("Predicted Gender Attitude") legend(order(1 "Prior to 1970" 2 "1970 onward")) xlabel(-3.5(.5)3) ylabel(-.75(.25).5) yline(0, lpattern(shortdash))
